package Client;
import java.io.IOException;
import java.io.RandomAccessFile;

public final class ObjectDef {
    private static RandomAccessFile objectDefFile;
    private static int[] indexes;
    private static ObjectDef[] cache = new ObjectDef[10];
    private static int cacheIndex;
    private int id;
    private String name;
    private String option1;
    private String option2;
    private int spriteId = -1;

    public int id() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
   }

    public String name() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String option1() {
        return option1;
    }

    public void setOption1(String option1) {
        this.option1 = option1;
    }

    public String option2() {
        return option2;
    }

    public void setOption2(String option2) {
        this.option2 = option2;
    }

    public int spriteId() {
        return spriteId;
    }

    public void setSpriteId(int spriteId) {
        this.spriteId = spriteId;
    }

    public static ObjectDef getObjectDef(int id) {
        try {
            for (int i = 0; i < cache.length; i++) {
                if (cache[i] != null && cache[i].id() == id) {
                    return cache[i];
                }
            }
            cacheIndex = (cacheIndex + 1) % cache.length;
            ObjectDef def = cache[cacheIndex] = new ObjectDef();
            def.setId(id);
            objectDefFile.seek(indexes[id]);
            boolean done = false;
            while (true) {
                int b;
                switch (objectDefFile.readUnsignedByte()) {
                case 0:
                    return def;
                case 1:
                    String name = "";
                    while ((b = objectDefFile.readByte()) != 0) {
                        name += (char) b;
                    }
                    def.setName(name);
                    break;
                case 2:
                    String option1 = "";
                    while ((b = objectDefFile.readByte()) != 0) {
                        option1 += (char) b;
                    }
                    def.setOption1(option1);
                    break;
                case 3:
                    String option2 = "";
                    while ((b = objectDefFile.readByte()) != 0) {
                        option2 += (char) b;
                    }
                    def.setOption2(option2);
                    break;
                case 4:
                    def.setSpriteId(objectDefFile.readUnsignedShort() - 1);
                    break;
                }
            }
        } catch (IOException ioe) {
        }
        return null;
    }

    public static void unpackConfig() {
        try {
            objectDefFile = new RandomAccessFile("./cache/objectdef.dat", "r");
            int totalObjects = objectDefFile.readUnsignedShort();
            indexes = new int[totalObjects];
            for (int i2 = 0; i2 < totalObjects; i2++) {
                indexes[i2] = objectDefFile.readUnsignedShort() + totalObjects * 2;
            }
        } catch (IOException ioe) {
        }
    }
}